<!--
**********************************************************
* @Author: 张小平
* @Date:   2024-05-01
* @Email:  qqshuqian@163.com
* @Last modified: 最后修改日期
* @Description: 文件描述
**********************************************************
-->
<template>
  <!-- 添加或修改用户机构角色对话框 -->
  <el-dialog :title="!form.id ? '新增': '修改'"
             v-model="visible">
    <el-form ref="dataForm" :model="form" :rules="rules" label-width="80px">
      <el-row>
        <el-col>
          <el-form-item label="部门">

            <avue-input-tree default-expand-all
                             checkStrictly
                             v-model="form.orgId" placeholder="选择部门"
                             :dic="deptOptions"></avue-input-tree>


<!--            <treeselect v-model="form.orgId"-->
<!--                        :options="deptOptions"-->
<!--                        :normalizer="normalizer"-->
<!--                        :show-count="true"-->
<!--                        placeholder="选择部门"-->
<!--            />-->
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col>
          <el-form-item label="角色">

            <avue-input-tree default-expand-all
                             checkStrictly
                             v-model="form.roleId" placeholder="选择角色"
                             :dic="roleOptions"></avue-input-tree>

<!--            <treeselect v-model="form.roleId"-->
<!--                        :options="roleOptions"-->
<!--                        :normalizer="normalizer"-->
<!--                        :show-count="true"-->
<!--                        placeholder="选择角色"-->
<!--            />-->
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col>
          <el-form-item label="权限">
            <el-select v-model="form.permission" placeholder="请选择权限">
              <el-option label="全部" value="all"></el-option>
              <el-option label="部门及其下属" value="dep"></el-option>
              <el-option label="本部门" value="depself"></el-option>
              <el-option label="本人" value="self"></el-option>
            </el-select>

          </el-form-item>
        </el-col>
      </el-row>


      <el-row>
        <el-col>
          <el-form-item label="开始时间">
            <el-date-picker
                    v-model="form.ksjs"
                    align="right"
                    type="date"
                    placeholder="选择日期"
                    :picker-options="pickerOptions">
            </el-date-picker>

          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col>
          <el-form-item label="结束时间">

            <el-date-picker
                    v-model="form.jsjs"
                    align="right"
                    type="date"
                    placeholder="选择日期"
                    :picker-options="pickerOptions">
            </el-date-picker>


          </el-form-item>
        </el-col>
      </el-row>




    <el-row>
      <el-col>
        <el-form-item label="代理人">
          <el-input v-model="form.dlr"  placeholder="请输入代理人"></el-input>
        </el-form-item>
      </el-col>
    </el-row>


    </el-form>



    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {fetchTree as fetchDeptTree} from '@/framework-admin/api/admin/dept'
import {listCore as fetchRoleTree} from "@/framework-core/api/core/api";
import {setOrgRole3} from "@/framework-admin/api/admin/user";
import { convertDic} from '@/framework-core/utils/util'

import Treeselect from 'vue3-treeselect'
import 'vue3-treeselect/dist/vue3-treeselect.css'

export default {
  name: "DeptForm",
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 部门树选项
      deptOptions: [],
      // 部门树选项
      roleOptions: [],
      permissionOptions:[],
      // 是否显示弹出层
      visible: false,
      form: {
      },
      // 表单校验
      rules: {
        // name: [
        //   {required: true, message: "部门名称不能为空", trigger: "blur"}
        // ],
        // sort: [
        //   {required: true, message: "部门顺序不能为空", trigger: "blur"}
        // ]
      }
    };
  },
  methods: {
    init(isEdit,row,currentUserId) {
      this.form = {};
      this.form.userId = currentUserId;
      this.visible = true
      this.getTreeselectOrg();
      this.getTreeselectRole();
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields();
        var $that = this;
        if (isEdit) {
          debugger
          this.form.id = row.id
          this.form.orgId = row.orgId;
          this.form.roleId =row.roleId;
          this.form.permission= row.permission;
        } else {
          this.form.orgId = 0;
          this.form.roleId =0;
          this.form.permission ='self';
          this.form.id = undefined
        }
      })

    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          // if (this.form.id === undefined) {
          //   this.form.id = 0
          // }
          setOrgRole3(this.form).then(data => {
             this.$message.success('修改成功')
             this.visible = false
             this.$emit('refreshDataList')
           });
        }
      })
    },
    /** 查询部门下拉树结构 */
    getTreeselectOrg() {
      fetchDeptTree().then(response => {
        // this.deptOptions = []
        // const dept = {id: 0, name: '请选择部门',disabled:false, children: response.data.data.data};
        // this.deptOptions.push(dept)

        this.deptOptions = []
        const menu = {value: 0, label: "请选择部门", children: []};
        convertDic(response.data.data.data,menu.children);
        this.deptOptions.push(menu);

      });
    },
    /** 查询部门下拉树结构 */
    getTreeselectRole() {
      fetchRoleTree("sys/role",{}).then(response => {
        this.roleOptions = []
        const menu = {value: 0, label: "请选择角色", children: []};
        convertDic(response.data.data.data,menu.children);
        this.roleOptions.push(menu);
      })


    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      }
    }
  }
};
</script>
